

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class SettingsWindow extends JPanel implements ActionListener, KeyListener{
	private static final long serialVersionUID = 1L;
	private static SettingsWindow mSettingsWindow = null;
	
	//I don't think this is necessary
	//private StatusWindow mStatusWindow = new StatusWindow(this); 
	
	private JCheckBox checkBox; //A list of all the available styles 
		
	private JPanel userNamePanel; 
	private JPanel colorChooserPanel;
	private JPanel checkBoxPanel; 
	
	private JLabel statusLabel = new JLabel("Current Status"); 
	private JTextField mStatus = new JTextField(); 
	//PLPLayers[] allPlayers; 
	private String[] imitationPlayers = {"Nick"}; 
	
	//PLPLayer currentUser; 
	private String currentUser;
	
	//PLPlayer currentPlayer; 

	
	SettingsWindow(){
		setPreferredSize(new Dimension(500,150));
		setLayout(new GridBagLayout());
		GridBagConstraints c1 = new GridBagConstraints(); 
		c1.fill = GridBagConstraints.HORIZONTAL; 
		
		JLabel textLabel = new JLabel ("User Name: "); 
		textLabel.setSize(new Dimension (150, 50)); 
		JTextField userName = new JTextField(); 
		
		userName.setEditable(true);
		userName.setPreferredSize(new Dimension (125,25)); 
		userName.addKeyListener(this); 
		userNamePanel = new JPanel(); 
		userNamePanel.setLayout(new GridLayout(2,1)); 
		userNamePanel.add(textLabel);
		userNamePanel.add(userName); 
		
		
		JPanel stylePanel = new JPanel(new GridBagLayout()); 
		stylePanel.setBorder(BorderFactory.createTitledBorder("Please select the artist's style you want to collaborate with")); 
		GridBagConstraints c = new GridBagConstraints(); 
		c.fill = GridBagConstraints.HORIZONTAL; 
		for (int i = 0; i < imitationPlayers.length; i++){
			c.gridx = 0; 
			c.gridy = i; 
			c.anchor= GridBagConstraints.LINE_START; 
			checkBox = new JCheckBox(imitationPlayers[i]); 
			c.fill = GridBagConstraints.HORIZONTAL; 
			c.anchor = GridBagConstraints.CENTER; 
			stylePanel.add(checkBox, c); 

		}
		//set up status panel
		mStatus.setEditable(false); 
		mStatus.setText("..."); 
		mStatus.setPreferredSize(new Dimension (150,25));
		JPanel statusPanel = new JPanel(); 
		statusPanel.setLayout(new GridLayout(2,1)); 
		statusPanel.add(statusLabel); 
		statusPanel.add(mStatus); 
		
		c1.fill = GridBagConstraints.HORIZONTAL; 
		c1.gridx = 0; 
		c.gridy = 0; 
		this.add(statusPanel, c1); 
		
		c1.fill = GridBagConstraints.HORIZONTAL; 
		c1.gridx = 1; 
		c1.gridy = 0; 
		//c1.ipadx = 30; 
		this.add(userNamePanel, c1); 

		c1.fill = GridBagConstraints.HORIZONTAL; 
		c1.gridx = 3; 
		c1.ipadx = 160; 
		c1.gridwidth = 6; 
		this.add(new JScrollPane(stylePanel), c1); 
	}

	public static synchronized SettingsWindow Obj() {
		if (mSettingsWindow == null) {
			mSettingsWindow = new SettingsWindow();
		}
		return mSettingsWindow;
	}

	// no cloning allowed
	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}	


	public void actionPerformed(ActionEvent e) {
		}
	public void setStatus(String status){
		mStatus.setText(status); 
	}
	
	public boolean updatePlayersDisplay(){
		//if a player has been added to the library while the player is playing a game (somehow) then this function will be able to add the new player to the list
		checkBoxPanel.removeAll();
		for (int i = 0; i < imitationPlayers.length; i++){
			checkBox = new JCheckBox(imitationPlayers[i]); 
			checkBoxPanel.add(checkBox); 
			checkBoxPanel.validate(); 
		}
		return true; 
		
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
	     if (key == KeyEvent.VK_ENTER) {
	        JTextField textField = (JTextField) e.getSource();
			setCurrentUser(textField.getText());
	        PLDebug.Info("User ="  + currentUser);  
	        }		
		// TODO Auto-generated method stub
		
	}
	public void setCurrentUser(String currentUser) {this.currentUser = currentUser;}
	
	public String getCurrentUser(){return currentUser;}; 

	public Color genColor(){
		Random colorGen = new Random(); 
		Color color = new Color(colorGen.nextInt(256), colorGen.nextInt(256), colorGen.nextInt(256));
		return color; 
	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	


	
}
